/*
 * @Author: PanZhangOne
 * @Date: 2017-12-17 13:43:25
 * @Last Modified by: PanZhangOne
 * @Last Modified time: 2017-12-17 18:25:21
 */
const express = require('express');
const router = express.Router();
const passport = require('passport');
const isAdmin = require('../middleware/isAdmin');
require('../passport')(passport);

const userService = require('../service/user');
const systemService = require('../service/system');
const topicTagService = require('../service/topicTag');
const topicService = require('../service/topic');

/**
 * 后台管理用户相关
 */

// 获取用户列表
router.get('/users', passport.authenticate('bearer', { session: false }), isAdmin, async (req, res) => {
  try {
    let users = await userService.getUserList();
    res.json({ success: true, message: '获取用户列表成功', data: users });
  } catch (e) {
    res.status(500).json({ success: false, message: '获取用户列表失败', info: e });
  }
});

// 获取最新会员信息 count 代表获取多少条
router.get('/users/new/:count', passport.authenticate('bearer', { session: false }), isAdmin, async (req, res) => {
  try {
    let users = await userService.getSomeUsers(Number.parseInt(req.params.count));
    res.json({ success: true, message: '查询成功', data: users });
  } catch (e) {
    res.status(500).json({ success: false, message: '查询失败', info: e });
  }
});

// 根据时间获取用户
router.get('/users/:start/:end', passport.authenticate('bearer', { session: false }), isAdmin, async (req, res) => {
  try {
    let users = await userService.getSometimeNewUser(null, req.params.start, req.params.end);
    res.json({ success: true, message: '获取用户列表成功', data: users });
  } catch (e) {
    res.status(500).json({ success: false, message: '获取用户列表失败', info: e });
  }
});

// 根据时间获取用户数量
router.get('/users/count/:start/:end', passport.authenticate('bearer', { session: false }), isAdmin, async (req, res) => {
  try {
    let usersCount = await userService.getSometimeNewUser('count', req.params.start, req.params.end);
    res.json({ success: true, message: '获取用户数量成功', data: usersCount });
  } catch (e) {
    res.status(500).json({ success: false, message: '获取用户数量失败', info: e });
  }
});

/**
 * 后台管理主题相关
 */

// 创建新主题
router.post('/topic', passport.authenticate('bearer', { session: false }), isAdmin, async (req, res) => {
  try {
    let topic = await topicService.createTopic({
      title: req.body.title,
      content: req.body.content,
      tag: req.body.tag,
      authorId: req.body.authorId
    });

    res.json({ success: true, message: '创建主题成功', data: topic });
  } catch (e) {
    res.status(500).json({ success: false, message: '创建主题失败', info: e });
  }
});

// 新建tag
router.post('/topic/tag', passport.authenticate('bearer', { session: false }), isAdmin, async (req, res) => {
  try {
    let tag = await topicTagService.createTopicTag(req.body.name);
    res.json({ success: true, message: '创建标签成功', data: tag });
  } catch (e) {
    res.status(500).json({ success: false, message: '创建标签失败', info: e });
  }
});

/**
 * 后台管理其他信息
 */

// 获取系统信息
router.get('/system', passport.authenticate('bearer', { session: false }), isAdmin, async (req, res) => {
  try {
    let systemData = await systemService.systemInfo();
    res.json({ success: true, message: '获取成功', data: systemData });
  } catch (e) {
    res.status(500).json({ success: false, message: '获取信息失败', info: e });
  }
});

module.exports = router;
